package com.zw.pdm.module.production.service;

import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.production.MakeChange;
import com.zw.pdm.entity.production.MakePlan;
import com.zw.pdm.entity.production.MakeSheet;
import com.zw.pdm.entity.rep.VerifyProcess;
import com.zw.pdm.module.production.mapper.MakeChangeMapper;
import com.zw.pdm.module.production.mapper.MakePlanMapper;
import com.zw.pdm.module.production.mapper.MakeSheetMapper;
import com.zw.pdm.module.production.model.MakeChangeModel;
import com.zw.pdm.module.production.model.MakeResultModel;
import com.zw.pdm.module.report.mapper.ChangeMapper;
import com.zw.pdm.module.report.mapper.PlanMapper;
import com.zw.pdm.module.report.mapper.SheetMapper;
import com.zw.pdm.module.report.mapper.VerifyProcessMapper;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.CommonUtils;
import com.zw.pdm.util.FileUtils;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName ProductionSheetService.java
 * @Description TODO
 * @createTime 2020年10月06日 16:43:00
 */
@Service
@SuppressWarnings("all")
public class ProductionSheetService {


    @Autowired
    private MakeSheetMapper makeSheetMapper;
    @Autowired
    private MakePlanMapper makePlanMapper;
    @Autowired
    private MakeChangeMapper makeChangeMapper;
/************************************************************************************************************/
    @Autowired
    private SheetMapper sheetMapper;
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private VerifyProcessMapper verifyProcessMapper;
    @Autowired
    private ChangeMapper changeMapper;

    @Value("${user.upload.path}")
    private String uploadPath;
    @Autowired
    private LogService logService;

    /** myf按ID查询制作件主表（make_sheet） */
    public MakeSheet getSheetById(Integer id) {
        return makeSheetMapper.getMakeOne(id);
    }


    /** myf按 制作件的sheet_id 查寻明细表（make_plan） */
    public List<MakePlan> findPlanBySheetId(Integer sheetId) {
        return makePlanMapper.findMakeBySheetId(sheetId);
    }



    /** 按 变更单ID 查询制作件计划清单 */
    public List<MakeChange> findChangeBySheetId(Integer sheetId) {
        return makeChangeMapper.findBySheetId(sheetId);
    }

    /** myf查询此制作件清单的所有审核流程 */
    public List<VerifyProcess> findVpBySheetId(Integer sheetId) {
        return verifyProcessMapper.findVpMakeBySheetId(sheetId);
    }

    /** myf上传临时文件 */
    public String uploadMakeTemp(MultipartFile file) {
        if (null != file) {
            StringBuffer sb = new StringBuffer();
            sb.append(uploadPath);
            sb.append(ShiroUtils.getLoginName());
            sb.append("/temp/");
            String fileName = FileUtils.upload(file, sb.toString());
            if (CommonUtils.isNotNull(fileName)) {
                logService.saveLog("上传附件", "名称：" + fileName);
                return sb.toString() + fileName;
            }
        }
        return null;
    }

    /** 根据项目号查询部分增加随机备件和备品备件
     *  暂时不用
     *
     * */
    @Deprecated
    public List<DsnProStruct> findMakePartByProjectCodea(String code,String type) {
        List<DsnProStruct> dsnProStructs = makeSheetMapper.findMakePartByProjectCode(code, type);
        DsnProStruct dsn1 = new DsnProStruct();
        dsn1.setProPart("随机备件");
        dsn1.setId(-88);
        dsnProStructs.add(dsn1);
        DsnProStruct dsn2 = new DsnProStruct();
        dsn2.setProPart("备品备件");
        dsn2.setId(-99);
        dsnProStructs.add(dsn2);
        return dsnProStructs;
    }

     /** myf根据项目编号和专业类型查询部分 */
    public List<DsnProStruct> findMakePartByProjectCode(String code,String type) {
        return makeSheetMapper.findMakePartByProjectCode(code,type);
    }

    /** myf根据项目编号和专业类型查询部分 */
    public List<DsnProStruct> findDrawPartByProjectCode(String code,String type) {
        /** 如果图纸下发选的是外观的话查询该项目下的所有部分，63是外观Id*/
        if(type.equals("63")){
            type=null;
        }
        List<DsnProStruct> makePartByProjectCode = makeSheetMapper.findMakePartByProjectCode(code, type);
        for (DsnProStruct dsnProStruct : makePartByProjectCode) {
            dsnProStruct.setProPart(dsnProStruct.getSpecial()+"--"+dsnProStruct.getProPart());
        }
        return makePartByProjectCode;
    }

    /** 通过sheetId查询makeResult表里面的数据*/
    public List<MakeResultModel> getMakeResultSheetId(Integer sheetId) {
        return makeSheetMapper.findMakeChangeResultSheetId(sheetId);
    }

    /** myf如果有删除的时候需要展示的数据请调用此方法：查看明细的方法包含变更删除的数据*/
    public List<MakeChangeModel> findMakeResultChangeBySheetId(Integer sheetId) {

        return makeSheetMapper.findMakeResultChangeBySheetId(sheetId);
    }

    /** myf根据手动输入的项目编号模糊查询设计类型的项目信息*/
    public List<Project> findDesignProjectByCode(String q) {
        return makeSheetMapper.findDesignProjectByCode(q);
    }
}
